package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.NoteAttachmentsDao;
import com.rstrulak.dao.NoteDao;
import com.rstrulak.model.Note;
import com.rstrulak.model.NoteAttachments;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;
import com.rstrulak.utils.NoteFile;

@Component("newNoteBean")
@Scope("request")
public class NewNoteBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private NoteDao noteDao;

	@Autowired
	private NoteAttachmentsDao noteAttachmentsDao;

	private Note note;

	private String noteName;

	private String noteDescription;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
	}

	public Note getNote() {
		return note;
	}

	public void setNote(Note note) {
		this.note = note;
	}

	public String getNoteName() {
		return noteName;
	}

	public void setNoteName(String noteName) {
		this.noteName = noteName;
	}

	public String getNoteDescription() {
		return noteDescription;
	}

	public void setNoteDescription(String noteDescription) {
		this.noteDescription = noteDescription;
	}

	public void saveNote(FileUploadBean fileUploadBean) throws ServletException, IOException {
		FileUploadBean uploadBean = fileUploadBean;
		ArrayList<NoteFile> filesArray = uploadBean.getFiles();
		List<NoteAttachments> noteAttachmentsList = new ArrayList<NoteAttachments>();
		note = new Note();

		note.setName(noteName);
		note.setDescription(noteDescription);
		note.setOwner(JSFUtils.getLoggedUser());
		note.setIsFinished(Boolean.FALSE);
		note.setStartDate(new Date());

		Long savedNoteId = noteDao.save(note);

		for (int i = 0; i < filesArray.size(); i++) {
			NoteAttachments noteAttachments = new NoteAttachments();
			String oldName = filesArray.get(i).getName();
			StringBuilder finalName = new StringBuilder();
			finalName.append("noteId_");
			finalName.append(savedNoteId);
			finalName.append("_fileName_");
			finalName.append(oldName);

			noteAttachments.setNote(note);
			noteAttachments.setFilePath(Constants.NOTE_ATTACHMENTS_PATH + finalName);
			noteAttachments.setFileName(oldName);
			noteAttachmentsList.add(noteAttachments);

			filesArray.get(i).setName(finalName.toString());
		}

		fileUploadBean.saveNoteFiles();
		fileUploadBean.clearUploadData();
		if (noteAttachmentsList.size() > 0) {
			this.saveNoteAttachments(noteAttachmentsList);
		}

		JSFUtils.redirect("/protected/note.xhtml");
	}

	public void cancelAddingNote(FileUploadBean fileUploadBean) throws ServletException, IOException {
		fileUploadBean.clearUploadData();
		JSFUtils.redirect("/protected/note.xhtml");
	}

	public void saveNoteAttachments(List<NoteAttachments> noteAttachmentsList) {
		for (NoteAttachments noteAttachments : noteAttachmentsList) {
			noteAttachmentsDao.save(noteAttachments);
		}
	}

}
